package com.web.controller;

import com.aop.SystemLog;
import com.dto.BaseDto;
import com.entity.Condition;
import com.entity.Dietitian;
import com.page.PageInfo;
import com.web.service.DietitianService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("/dietitianController")
public class DietitianController {
    @Resource
    DietitianService dietitianService;

//    @RequestMapping("/dietitians")
//    @ResponseBody
//    public BaseDto<Dietitian> getAll() throws Exception {
//        return dietitianService.getAll();
//    }



    @SystemLog(module = "营养师管理", methods = "分页查询")
    @RequestMapping("/dietitians/page/json")
    @ResponseBody
    public BaseDto<Dietitian> getDietitiansByPageJson(PageInfo pageInfo) throws Exception {
        String cacheId = "DietitianByPage" + pageInfo.getCurrPage();//自编redis每页数据缓存的key
        return pageInfo.getCurrPage() > 2 ? dietitianService.getDietitiansByPage(pageInfo) :
                dietitianService.getDietitiansByPage(pageInfo, cacheId);
    }


    /**
     * 根据用户提供的用户名模糊搜索找到 用户编号集合
     * @param petternName
     * @return
     * @throws Exception
     */
    @RequestMapping("/dids/search/{petternName}")
    @ResponseBody
        public BaseDto<String> searchDietitianIdsByName(@PathVariable("petternName") String petternName) throws Exception {
            return dietitianService.searchDietitianIdsByName(petternName);
        }

    @RequestMapping("/dietitians/page")
    public ModelAndView getDietitiansByPage(PageInfo pageInfo) throws Exception {
        BaseDto<Dietitian> dto = dietitianService.getDietitiansByPage(pageInfo);

        return new ModelAndView("/back/dietitian/list", "dto", dto);
    }

    //@RequestMapping(name = "/dietitian",method = RequestMethod.POST)
    @PostMapping("/dietitian")
    @ResponseBody
    public BaseDto<Dietitian> add(Dietitian dietitian) throws Exception {
        return dietitianService.add(dietitian);
    }





}
